package com.cheejee.goodscenter.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cheejee.goodscenter.dao.SkuCategoryPropertiesMapper;
import com.cheejee.goodscenter.module.entity.SkuBrandProperties;
import com.cheejee.goodscenter.module.entity.SkuCategoryProperties;
import com.cheejee.goodscenter.service.SkuCategoryPropertiesService;
import com.cheejee.goodscenter.service.exception.DataNotExistException;
import com.cheejee.goodscenter.validated.Check;
import com.cheejee.goodscenter.validated.InsertGroup;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

/**
 * @author CARRY
 * @Date 2020年10月29日下午1:16:30
 */
@Service
public class SkuCategoryPropertiesServiceImpl implements SkuCategoryPropertiesService {

	@Resource
	private SkuCategoryPropertiesMapper scpm;

	@Override
	public SkuCategoryProperties queryById(Long id) {

		return scpm.selectByPrimaryKey(id);
	}

	@Override
	public Page<SkuCategoryProperties> querySkuCategoryProperties(Long skuId, Integer categoryPropertiesTemplateId, String propertiesName, int pageNum, int pageSize) {

		return PageHelper.startPage(pageNum, pageSize).doSelectPage(() -> scpm.selectBySelective(skuId, categoryPropertiesTemplateId, propertiesName));
	}

	@Override
	@Check(message = "新增的SKU分类属性不能为空", group = InsertGroup.class)
	public int addSkuCategoryProperties(SkuCategoryProperties properties) {

		return scpm.insertSelective(properties);
	}
	
	@Override
	@Check(message = "新增的SKU分类属性不能为空", group = InsertGroup.class)
	public int addSkuCategoryProperties(List<SkuCategoryProperties> properties) {
		
		return scpm.insertMany(properties);
	}

	@Override
	public int changeSkuCategoryPropertiesValue(Long propertiesId, String propertiesValue){
		checkExist(propertiesId);
		SkuCategoryProperties properties = new SkuCategoryProperties();
		properties.setId(propertiesId);
		properties.setPropertiesValue(propertiesValue);

		return scpm.updateByPrimaryKeySelective(properties);
	}

	@Override
	public int deleteSkuCategoryProperties(Long... id) {

		return scpm.deleteById(id);
	}
	
	private void checkExist(Long id) {
		if(scpm.selectByPrimaryKey(id) == null) {
			throw new DataNotExistException("SKU分类属性不存在");
		}
	}
	
	@Override
	public int deleteSkuCategoryPropertiesBySku(Long skuId) {
		
		return scpm.deleteBySku(skuId);
	}
	
}